package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--修改成品备课包
 * 
 */
@CrossOrigin
@RestController
public class LectureUptNamePrePackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @ApiOperation(value = "老师 ---备授课--修改成品备课包")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/name/upd", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String userNick = getProfile(request, response).getUserNick();
        String schoolName = getProfile(request, response).getSchoolName();
        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");
        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository
                .findByIdAndUserIdAndSchoolIdAndIsDeleteNot(req.id, userId, schoolId, 1);
        if (yxyLecturePrePack != null) {
            yxyLecturePrePack.setReferencUserId(userId);
            yxyLecturePrePack.setUserNick(userNick);
            yxyLecturePrePack.setPackName(req.packName);
            yxyLecturePrePack.setSchoolNick(schoolName);
            yxyLecturePrePackRepository.save(yxyLecturePrePack);
        }
        respModel = new RespModel();
        respModel.setMessage("成品备课包更新成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String id;

        public String packName;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "备课包ID不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(packName, null), "请填写备课包名称");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
